package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.entity.activity.dunhuangshiku.DunHuangShiKuEventConfig;
import com.yanqu.road.entity.enums.eDunHuangShiKuEventType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDunHuangShiKuEventAction;
import com.yanqu.road.logic.pb.DunHuangPb;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.dunhuangshiku.DunHuangShiKuModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_ACTION_FROM_CROSS, desc = "敦煌石窟0攻击 1接受 2绕过 3标记 4下一个区域 跨服返回区服 协议")
public class DunHuangShikuActionFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuActionToGameServerMsg resp = DunHuangShiKuProto.DunHuangShiKuActionToGameServerMsg.parseFrom(packet.getBytes());
        ret = resp.getRet();
        DunHuangShiKuModule dunHuangShiKuModule = player.getModule(DunHuangShiKuModule.class);
        // 取消进入下一个区域预扣除
        Property nextRegionProperty = null;
        if(resp.getType() == 4 && dunHuangShiKuModule.nextRegionProperty != null){
            nextRegionProperty = dunHuangShiKuModule.nextRegionProperty;
            player.getModule(PreReduceModule.class).restorePreReduce(dunHuangShiKuModule.nextRegionProperty);
            dunHuangShiKuModule.nextRegionProperty = null;
        }
        DunHuangShiKuProto.DunHuangShiKuActionRespMsg.Builder builder = DunHuangShiKuProto.DunHuangShiKuActionRespMsg.newBuilder();
        builder.setRet(ret);
        int touchMallId = 0;
        if(ret == 0){
            dunHuangShiKuModule.regionId = resp.getLog().getRegionId();
            dunHuangShiKuModule.teamId = resp.getLog().getTeamId();

            DunHuangShiKuEventConfig eventConfig = DunHuangShiKuMgr.eventConfigMap.get(resp.getEventId());
            StringBuilder rewardSb = new StringBuilder();
            if(resp.getType() == 0 || resp.getType() == 1){
                    // 攻击  获得奖励 积分 排行
                    // 接受 获得奖励 积分 排行
                if(eventConfig.getEventType() == eDunHuangShiKuEventType.SutraCave.getValue()){
                    int now = DateHelper.getCurrentSecond();
                    touchMallId = DunHuangShiKuMgr.randomMall(dunHuangShiKuModule,player,now);
                    if(touchMallId > 0){
                        int times = dunHuangShiKuModule.baseData.getTouchMallTimes(touchMallId);
                        DunHuangShiKuProto.DunHuangShiKuUserMallInfo.Builder msg = DunHuangPb.parseDunHuangShiKuUserMallInfo(touchMallId, now, times);
                        builder.setMallInfo(msg);
                    }
                }
                CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
                Property reward = PropertyHelper.parseStringToProperty(resp.getReward());
                if(reward != null){
                    builder.setReward(resp.getReward());
                    rewardSb.append(builder.getReward());
                    currencyModule.addCurrency(reward, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuFinishEventReward);
                }
                // 加积分
                if(resp.getScore() > 0){
                    if(rewardSb.length() > 0){
                        rewardSb.append(";");
                    }
                    Property score = new Property(DunHuangShiKuMgr.DUNHUANGSHIKU_SCORE_ITEM_ID, BigInteger.valueOf(resp.getScore()));
                    rewardSb.append(PropertyHelper.parsePropertyToString(score));
                }
                if(resp.getAllScore() > 0){
                    player.notifyListener(eGamePlayerEventType.DunHuangShiKuScoreCrossRank.getValue(), resp.getAllScore());
                }
                builder.setBookNum(resp.getBookNum());
                builder.setDamage(resp.getDamage());
                builder.setBlood(resp.getBlood());
                builder.setScore(resp.getScore());
            }else if(resp.getType() == 4){
                // 下一个区域
                if(nextRegionProperty != null) {
                    // 扣道具
                    player.getModule(CurrencyModule.class).removeCurrency(nextRegionProperty, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuUseBookCost);
                }
                // 文书数量
                builder.setBookNum(resp.getBookNum());
            }
            // 加日志
            if(resp.getType() < 4){
                long targetId = touchMallId == 0 ? resp.getLog().getTargetId() : touchMallId;
                long damage = resp.getLog().getBeforeAttackBlood() - resp.getLog().getAfterAttackBlood();
                AutoLogMgr.add(new LogDunHuangShiKuEventAction(DunHuangShiKuMgr.activityInfo.getActivityId(), player.getUserId(), resp.getLog().getTeamId(), resp.getLog().getRegionId(), resp.getLog().getEventType(), resp.getType(), targetId, resp.getLog().getStatus(), resp.getLog().getBeforeAttackBlood(), resp.getLog().getAfterAttackBlood(), damage, rewardSb.toString()));
            }
        }
        player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_ACTION, builder);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
